//
//  SFPMaternityNotesController.swift
//  StriveForPregnancy
//
//  Created by iOS-Czz on 2023/11/9.
//

import UIKit

class SFPMaternityNotesController: SFPBaseViewController {
    @IBOutlet var tableView: UITableView!
    lazy var viewModel: SFPMaternityNotesViewModel = {
        return SFPMaternityNotesViewModel()
    }()
    @IBOutlet var totalLabel: UILabel!
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        loadData()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        setTab()
        self.addRightBarButton(imageName:"icon_add")
       
    }
    @IBAction func gotoAddNote(_ sender: Any) {
        self.navigationController?.pushViewController(SFPAddMaternityNotesController(), animated: true)
    }
    func setTab(){
        self.tableView.dataSource = self
        self.tableView.delegate = self
        self.tableView.register(UINib(nibName: "SFPMaternityNotesCell", bundle: .main), forCellReuseIdentifier: "SFPMaternityNotesCell")
    }
    func loadData(){
        self.viewModel.getPageData() { models in
            self.totalLabel.text = self.viewModel.totals
            self.tableView.reloadData()
        }
    }
    
}
extension SFPMaternityNotesController:UITableViewDataSource,UITableViewDelegate{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.viewModel.dataSource.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:SFPMaternityNotesCell = tableView.dequeueReusableCell(withIdentifier: "SFPMaternityNotesCell") as! SFPMaternityNotesCell
        cell.model = self.viewModel.dataSource[indexPath.row]
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
}
